/**
 * @author Matt Brophy (matt@brophy.org)
 */

if (typeof org == "undefined" || 
    org == null) { org = {}; }
if (typeof org.brophy == "undefined" || 
    org.brophy == null) { org.brophy = {}; }
if (typeof org.brophy.js == "undefined" || 
    org.brophy.js == null) { org.brophy.js = {}; }

org.brophy.js.demo = {};

org.brophy.js.demo.drawPageHeader = function(title)
{
  var header = MochiKit.DOM.H2(null,
    "Welcome to the demonstration page for the " + 
    "org.brophy.js." + title + " JavaScript library");

  var p1 = MochiKit.DOM.P(null,
    "This page contains a bunch of small, lightweight " + 
    "demo's that show the functionality and usage of " + 
    "functions in the org.brophy.js." + title + " library.");

  var p2 = MochiKit.DOM.P(null,
    "Each function will show you a short snippet of " + 
    "the JavaScript and and/or HTML code required to " + 
    "use the function.  Please note that this code " + 
    "being displayed on the page is being passed to " + 
    "eval() or appended directly into the document, so " + 
    "the examples shown are exactly the code you see " + 
    "being executed!");

  var demoDiv = MochiKit.DOM.DIV({'id':'demoDiv'},null);

  MochiKit.DOM.appendChildNodes(document.body,
                                header,
                                p1,
                                p2,
                                demoDiv);

}

org.brophy.js.demo.drawDemoHeader = function(title,id)
{
  MochiKit.DOM.appendChildNodes(
    MochiKit.DOM.getElement("demoDiv"),
    MochiKit.DOM.DIV( { "class" : "demo" },
      MochiKit.DOM.DIV( { "class"   : "title obj_link",
                          "onclick" : "org.brophy.js.util." + 
                          "toggleDisplay('" + id + "', false);" }, title),
      MochiKit.DOM.DIV( { "class" : "demo", "id" : id },null)));
}
                        
org.brophy.js.demo.getCodeDiv = function(codeStr)
{
  codeStr = codeStr.replace(/ /g, "\u00a0");
  var codeDiv = MochiKit.DOM.DIV( { "class" : "obj_code_container obj_code" }, null);
  
  var lines = codeStr.split("\n");
  
  MochiKit.DOM.appendChildNodes(codeDiv,
                   MochiKit.DOM.DIV( { "class" : "obj_code_line_odd" }, MochiKit.DOM.BR()));

  for(var i = 0; i < lines.length; i++)
  {
    var isEven = (i%2) == 0;
    var className = isEven ? "obj_code_line_even" : "obj_code_line_odd";

    var lineDiv = MochiKit.DOM.DIV( { "class" : className }, i+1 + ":" + org.brophy.js.constant.UNICODE_NBSP + lines[i]);
    MochiKit.DOM.appendChildNodes(codeDiv, lineDiv);
  }

  var className = (lines.length%2) == 0 ? 
                  "obj_code_line_even" : 
                  "obj_code_line_odd";
  MochiKit.DOM.appendChildNodes(codeDiv,
                   MochiKit.DOM.DIV( { "class" : className }, MochiKit.DOM.BR()));

  return codeDiv;
}

org.brophy.js.demo.drawDemoSection = 
function(title, id, htmlCode, jsCode, instructions)
{
  org.brophy.js.demo.drawDemoHeader(title, id);

  if(!MochiKit.Base.isUndefinedOrNull(htmlCode))
  {
    MochiKit.DOM.appendChildNodes(MochiKit.DOM.getElement(id),
                     MochiKit.DOM.P( { "class" : "obj_b obj_u demo" }, "HTML Code:"),
                     org.brophy.js.demo.getCodeDiv(htmlCode));
  }
     
  if(!MochiKit.Base.isUndefinedOrNull(jsCode))
  {
    MochiKit.DOM.appendChildNodes(MochiKit.DOM.getElement(id),
                     MochiKit.DOM.P( { "class" : "obj_b obj_u demo" }, "JavaScript Code:"),
                     org.brophy.js.demo.getCodeDiv(jsCode));
  }

  MochiKit.DOM.getElement(id).innerHTML += 
    "<div class='demo'>" + 
    "  <div class='demo obj_b obj_u demo'>Example</div>" + 
    "  <div class='demo'>" + instructions + "</div>" + 
    "  <div class='demo'>" + htmlCode + "</div>" + 
    "</div>";

  //window.eval(jsCode);
  if(document.all) 
    { eval.apply(document.all, [ jsCode ]); }
  else 
    { eval.apply(window, [ jsCode ]); }
}
